package com.hsq.file_;


import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

public class TestSerializable {
    public static void main(String[] args) {
        Employee harry = new Employee("Harry Hacker", 50000);
        Manager manager1 = new Manager("Tony Tester", 80000);
        manager1.setSecretary(harry);
        Employee[] staff = new Employee[2];
        staff[0] = harry;
        staff[1] = manager1;
        try {
            ObjectOutputStream out = new ObjectOutputStream(
                    new FileOutputStream("D:\\git_turtle\\employee.dat")
            );
            out.writeObject(staff);
            out.close();
            ObjectInputStream in = new ObjectInputStream(
                    new FileInputStream("D:\\git_turtle\\employee.dat")
            );
            Employee[] newStaff = (Employee[]) in.readObject();
            in.close();
            newStaff[0].raiseSalary(10);
            for (int i = 0; i < newStaff.length; i ++) {
                System.out.println(newStaff[i]);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

class Employee implements Serializable {
    private String name;
    private double salary;

    public Employee(String name, double salary) {
        this.salary = salary;
        this.name = name;
    }
    public void  raiseSalary(double byPercent) {
        double raise = this.salary * byPercent / 100;
        salary += raise;
    }

    @Override
    public String toString() {
        return getClass().getName() +
                "name='" + name + '\'' +
                ", salary=" + salary +
                '}';
    }
}

class Manager extends Employee{
    private Employee secretary;//秘书

    public Manager(String name, double salary) {
        super(name, salary);
        secretary = null;
    }

    public void setSecretary(Employee secretary) {
        this.secretary = secretary;
    }
    //    public void setSecretary(Employee e) {
//        secretary = e;
//    }
    public String toString() {
        return super.toString() + "[secretary="
                + secretary + "]";
    }
}
